#!/usr/bin/make -f

# Need some reasonable defaults in case we're run standalone
srcdir ?= .
AM_DEFAULT_VERBOSITY ?= 0
NODE_ENV ?= production

WEBPACK_PACKAGES = \
	base1 \
	apps \
	kdump \
	metrics \
	networkmanager \
	packagekit \
	playground \
	selinux \
	shell \
	sosreport \
	static \
	storaged \
	systemd \
	tuned \
	users \
	$(NULL)

MANIFESTS = \
	$(WEBPACK_PACKAGES:%=dist/%/manifest.json) \
	pkg/pcp/manifest.json \
	pkg/ssh/manifest.json \
	$(NULL)

.PHONY: all-webpack
all-webpack: $(MANIFESTS)

V_TAR = $(V_TAR_$(V))
V_TAR_ = $(V_TAR_$(AM_DEFAULT_VERBOSITY))
V_TAR_0 = @echo "  TAR     " $@;

webpack-jumpstart.tar: $(MANIFESTS) package-lock.json tree
	$(V_TAR) tar cf webpack-jumpstart.tar dist package-lock.json tree

V_WEBPACK = $(V_WEBPACK_$(V))
V_WEBPACK_ = $(V_WEBPACK_$(AM_DEFAULT_VERBOSITY))
V_WEBPACK_0 = @echo "  WEBPACK  $(@:dist/%/manifest.json=%)";

WEBPACK_MAKE = NODE_ENV=$(NODE_ENV) SRCDIR=$(abspath $(srcdir)) $(srcdir)/tools/webpack-make

WEBPACK_CONFIG = $(srcdir)/webpack.config.js
WEBPACK_INPUTS =
WEBPACK_OUTPUTS =
WEBPACK_INSTALL =
WEBPACK_GZ_INSTALL =
WEBPACK_DEPS = $(WEBPACK_PACKAGES:%=$(srcdir)/dist/%/Makefile.deps)

noinst_SCRIPTS += $(MANIFESTS)
EXTRA_DIST += $(MANIFESTS) $(WEBPACK_DEPS) webpack.config.js

# Nothing generates this directly, but it's included as a dependency in
# various places.  It will automatically appear as part of the webpack
# build.
dist/%/Makefile.deps:
	@true

dist/%/manifest.json: $(srcdir)/tools/webpack-make $(srcdir)/package-lock.json $(WEBPACK_CONFIG)
	$(V_WEBPACK) $(WEBPACK_MAKE) -d dist/$*/Makefile.deps -c $(WEBPACK_CONFIG)

-include $(WEBPACK_DEPS)

V_NODE_MODULES = $(V_NODE_MODULES_$(V))
V_NODE_MODULES_ = $(V_NODE_MODULES_$(AM_DEFAULT_VERBOSITY))
V_NODE_MODULES_0 = @V=0

# We want tools/node-modules to run every time package-lock.json is requested
# See https://www.gnu.org/software/make/manual/html_node/Force-Targets.html
FORCE:
package-lock.json: FORCE
	$(V_NODE_MODULES) $(srcdir)/tools/node-modules make_package_lock_json

# Write the current git tree hash, and touch the file only if it changed.
# Make sure the tree is clean, first.  Gets included in the jumpstart tar.
tree: FORCE
	@test -z "$$(git status --porcelain)" || (git status && false)
	@git rev-parse HEAD^{tree} > tree.tmp && \
	if cmp -s tree tree.tmp; then \
		rm tree.tmp; \
	else \
		printf "  %-8s %s\n" GEN "tree  [$$(cat tree.tmp)]"; \
		mv tree.tmp tree; \
	fi
